package com.chenjun.common.poi;

import com.alibaba.fastjson.JSONObject;
import com.chenjun.common.poi.annotation.Excel;
import com.chenjun.common.poi.annotation.ExcelField;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.*;

import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * @author chenjun
 * @date 2018/7/6 0006
 */
public class POIUtil {
    public static <T> Workbook exportList(Workbook workbook, List<T> list, Class<T> tClass) {
        assert workbook != null;
        assert list != null;
        assert tClass != null;
        Excel classAnnotation = tClass.getAnnotation(Excel.class);
        Sheet sheet = workbook.createSheet(classAnnotation.sheetName());
        Field[] fields = tClass.getDeclaredFields();
        List<Field> fieldList = new ArrayList<>();
        for (Field field : fields) {
            if (field.getAnnotation(ExcelField.class) != null) {
                fieldList.add(field);
            }
        }
        fieldList.sort(Comparator.comparingInt(o -> o.getAnnotation(ExcelField.class).sortNumber()));
        //创建表头
        int rowNum = 0;
        Row row = sheet.createRow(rowNum++);
        CellStyle headStyle = workbook.createCellStyle();
        headStyle.setFillForegroundColor(classAnnotation.headColor().getIndex());
        headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        CellStyle oddStyle = workbook.createCellStyle();
        oddStyle.setFillForegroundColor(classAnnotation.oddRowColor().getIndex());
        oddStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        CellStyle evenStyle = workbook.createCellStyle();
        evenStyle.setFillForegroundColor(classAnnotation.evenRowColor().getIndex());
        evenStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        for (Field field : fieldList) {
            Cell cell = row.createCell(fieldList.indexOf(field));
            cell.setCellStyle(headStyle);
            cell.setCellValue(field.getAnnotation(ExcelField.class).fieldName());
        }
        for (T bean : list) {
            row = sheet.createRow(rowNum++);
            CellStyle cellStyle;
            if (rowNum % 2 == 0) {
                cellStyle = oddStyle;
            } else {
                cellStyle = evenStyle;
            }
            JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(bean));
            for (Field field : fieldList) {
                ExcelField excelField = field.getAnnotation(ExcelField.class);
                Cell cell = row.createCell(fieldList.indexOf(field));
                cell.setCellStyle(cellStyle);
                sheet.setColumnWidth(cell.getColumnIndex(), excelField.width());
                if (excelField.fieldType().equals(ExcelField.FieldType.STRING)) {
                    String string = json.getString(field.getName());
                    if (StringUtils.isBlank(string)) {
                        cell.setCellValue(excelField.nullConvert());
                    } else {
                        cell.setCellValue(string);
                    }
                } else if (excelField.fieldType().equals(ExcelField.FieldType.DATE)) {
                    Date jsonDate = json.getDate(field.getName());
                    if (jsonDate == null) {
                        cell.setCellValue(excelField.nullConvert());
                    } else {
                        cell.setCellValue(DateFormatUtils.format(jsonDate, excelField.dateFormatter()));
                    }
                } else {
                    Object number = json.get(field.getName());
                    if (number == null) {
                        cell.setCellValue(excelField.nullConvert());
                    } else {
                        cell.setCellValue(new DecimalFormat(excelField.numberFormatter()).format(number));
                    }
                }
            }
        }
        return workbook;
    }
}
